package cn.mall.product.service.impl;

import cn.mall.common.utils.PageUtils;
import cn.mall.common.utils.Query;
import cn.mall.product.dao.SkuInfoDao;
import cn.mall.product.dao.SpuImagesDao;
import cn.mall.product.entity.SkuImagesEntity;
import cn.mall.product.entity.SkuInfoEntity;
import cn.mall.product.entity.SpuImagesEntity;
import cn.mall.product.entity.SpuInfoDescEntity;
import cn.mall.product.service.*;
import cn.mall.product.vo.AttrGroupVo;
import cn.mall.product.vo.SkuItemSaleAttrVo;
import cn.mall.product.vo.SkuItemVo;
import cn.mall.common.vo.SkuItemsVo;
import cn.mall.product.vo.SpuImagesVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    private SkuImagesService imagesService;
    @Autowired
    private SpuInfoDescService infoDescService;
    @Autowired
    private AttrGroupService groupService;
    @Autowired
    private SkuSaleAttrValueService saleAttrValueService;
    @Autowired
    private SkuInfoDao dao;
    @Autowired
    private SpuImagesDao spuImagesDao;
    @Autowired
    private ThreadPoolExecutor executor;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            /**
             * sku基本信息  pms——sku-info
             */
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;

        }, executor);
        CompletableFuture<Void> saleFuture = infoFuture.thenAcceptAsync((res)->{
            /**
             *h获取销售属性总和
             */
            List<SkuItemSaleAttrVo> saleAttrVo = saleAttrValueService.getSaleAttrValueBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVo);

        }, executor);
        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res)->{
            /**
             * spu 商品介绍 spu_sku_infoZ_desc
             */
            SpuInfoDescEntity infoDesc = infoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(infoDesc);

        }, executor);
        CompletableFuture<Void> attrFuture = infoFuture.thenAcceptAsync((res)->{
            /**
             * 获取商品spu参数规格
             * 通过建立视图
             */
            List<AttrGroupVo> groupVos = groupService.getAttrGroupWithAttrBySkuId(res.getSpuId(), res.getCatalogId());
            SpuImagesVo imagesEntities = spuImagesDao.selectBySpuId(res.getSpuId());
            skuItemVo.setSpuImagesVo(imagesEntities);

            skuItemVo.setGroupAttrs(groupVos);

        }, executor);
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(()->{

            /**
             * sku 图片信息 pms_sku_image
             */
            List<SkuImagesEntity> images = imagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(images);


        }, executor);
        CompletableFuture.allOf(saleFuture,descFuture,attrFuture,imageFuture).get();






        return skuItemVo;
    }

    @Override
    public List<SkuItemsVo> skuItems() {
        return dao.searchAllSku();
    }

}